<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Node.js 事件循环</title>
    <!-- 引入 Bootstrap -->
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <style>
        img{max-width: 650px;}
    </style>
</head>
<body class="container">
<h1>Node.js 事件循环</h1>
<h3>创建app.js，内容如下：</h3>
<div class="well">
    <xmp>
//引入 events模块
var events=require('events');
//创建 eventEmitter对象
var eventEmitter=new events.EventEmitter();
//创建event处理程序
var connectHandler=function  connected() {
console.log('连接成功。');
//触发data_received事件
eventEmitter.emit('data_received');
}
//绑定connection事件处理程序
eventEmitter.on('connection',connectHandler);
//使用匿名函数绑定 data_received事件
eventEmitter.on('data_received',function () {
console.log('数据接收成功！');
})
//触发connection事件
eventEmitter.emit('connection');
console.log('程序执行完毕。')
    </xmp>
</div>
<h3>在cmd中运行效果如下：</h3>
<div class="well">
    <img src="20171112213943.png"/>
</div>
</body>
</html>